package com.ymatou.iapi.sellerproduct.testcase;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.ModifyProductPicCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.utils.EnvSetup;

public class Ts_ModifyProductPic {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("App-图片修改");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new ModifyProductPicCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ModifyProductPic_001() {
		Logger.start(true, "App-图片修改");
		try{
			List<String> picUrls = new ArrayList<String>();
			picUrls.add("http://p11.img.ymatou.com/G01/M01/00/E5/rBBk0VXIqXOAP8WIAAFuo51Y5lA11_o.jpg");
			picUrls.add("http://p22.img.ymatou.com/G01/M02/00/E5/rBBk0VXIqXOAP8WIAAFuo51Y5lA22_o.jpg");
			
			//1 add new product
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			//2 modify pic
			Map<String, Object> updateMap = new HashMap<String,Object>();
			updateMap.put("ProductId", productId);
			updateMap.put("ProductImgUrls", new JSONArray(picUrls));
			
			JSONObject modifyPicBean = new ModifyProductPicCall().callAndGetReturnData(updateMap);
			Logger.verifyEquals(200, modifyPicBean.getInt("Code"), "响应报文-返回码");
			
			
			SellerProductCallService.checkSQLAndMongo_ModifyPicture(productId,picUrls);
			
			NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			
		/*	//check SQL
			List<YmtProductsI> ymtProducts = new YmtProductsIWapper().selectProductByProductId(productId);
		    String sPicUrlbysql = ymtProducts.get(0).getsPicUrl();
		    Logger.verifyIsContains(sPicUrlbysql, picUrls, "SQL Ymt_Products 图片对比");
			
			List<Map> retList = new YmtProductPictureWapper().selectByProductId(productId);
			Logger.verifyEquals(true, retList.size()>2, "SQL-ProductPicture记录");
			
			int retActionIs1 = 0;
			for(int i=0;i<retList.size();i++){
				Map<String, Object> ret = retList.get(i);
				if (ret.get("iAction").toString().equals("0")){
					retActionIs1++;
					Logger.verifyEquals(true,picUrls.contains(ret.get("sOriUrl").toString()),"SQL-ProductPicture oriUrl");
					
				}
			}
			Logger.verifyEquals(2,retActionIs1,"SQL-ProductPicture 记录");
			
			
			//check Mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				Map<String, Object> productBasicInfoBean = new ProductBasicInfoWapper().getByProductId(productId);
				String retUrlStr = productBasicInfoBean.get("LivePicList").toString();
				for (String urlStr:picUrls){
					Logger.verifyEquals(true,retUrlStr.contains(urlStr),"Mongo-ProductBasicInfo LivePicList");
				}
			}
			
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				JSONObject productDetailModelBean = new JSONObject(new ProductDetailModelWapper().getProductId(productId).toString());
				JSONArray picList = productDetailModelBean.getJSONArray("PicList");
				Logger.verifyEquals(picUrls.size(), picList.length(), "Mongo-ProductDetailModel picList个数");
				for (int i=0;i<picList.length();i++){
					Logger.verifyEquals(true,picUrls.contains(picList.getString(i)),"Mongo-ProductDetailModel picList");
				}
				
				
				
			}
			*/
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	

}
